package com.quan.weixin.api.commons;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.bean.R;
import com.quan.common.core.MyBaseController;

import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

@AllArgsConstructor
@RestController
@RequestMapping("/api/wx/common")
public class WxCommonController extends MyBaseController {
	
	@Autowired
	private WxMpService wxMpService;
    
	/**
	 * JSAPI 签名
	 * @param jsapiSignature
	 * @return
	 */
	@PostMapping("/jsapi_signature")
	public Object sendSMSValidCode(@RequestBody WxJsapiSignature jsapiSignature) {
		
		try {
			WxJsapiSignature createJsapiSignature = this.wxMpService.createJsapiSignature(jsapiSignature.getUrl());
			
			logger.info("获取JSAPI签名：{}, {}, {}", 
					createJsapiSignature.getAppId(), 
					createJsapiSignature.getSignature(), 
					createJsapiSignature.getUrl());
			
			return R.ok(createJsapiSignature);
		} catch (WxErrorException e) {
			logger.error("生成JSAPI签名失败，请检查是否配置微信白名单：{}", e.getMessage());
			return R.failure("生成JSAPI签名失败");
		}
		
	}

}
